<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Models\Patient;
use App\Models\DoctorUser;
use App\Models\DoctorOrder;
use App\Models\DoctorGoods;
use App\Models\DoctorRole;

use App\Http\Controllers\Controller;
use Illuminate\Routing\Router;
use App\Models\WxUser;
use App\Models\TagImage;
use App\Models\TagTask;
use Request;
use Illuminate\Support\Facades\Log;

include_once "common.php";

class H6Controller extends Controller
{
    function test(){
        echo 'ss';
    }

    function getCookie(){
        $value = request()->cookie('token');
        if(!$value){
            return false;
        } else {
            return true;
        }
    }


    // 医生端H5 api 登录 post {mobile,password}
    function login(){
        $mobile = Request::input('mobile');
        $password = Request::input('password');
        $result = DoctorUser::where('mobile',$mobile)->get()->first();
        if(!$result){
            return $this->res(1,'用户名或密码错误');
        }
        $repassword = $result['password'];
        //test
        //$result = $this->get_goods_price("1,2,3");
        if($repassword == $password){
            return $this->res(0,'登录成功',$result);

        } else {
            return $this->res(1,'登录失败',$repassword);
        }      
    }

    // API 分润系数查询{role_id}
    function get_profit(){
        $role_id = Request::input('role_id');
        $result = DoctorRole::where('role_id',$role_id)->get()->first();
        return $this->res(0,'查询成功',$result);
    }
    //查询商品 返回价格 {id} 字符串
    function get_goods_price(){
        $id = Request::input('id');
        if(strstr($id,',')){
            $str = explode(",",$id);
        } else {
            $str = array($id);
        }
        //$str = explode(",",$id);
        $result = array();
        foreach($str as $val){
            $a = array();
            $goods = DoctorGoods::where('id',$val)->get()->first();
            $a['id'] = $val;
            $a['price'] = $goods['price'];
            $a['goods_name'] = $goods['goods_name'];
            array_push($result,$a);
        }
        return $this->res(0,'查询成功',$result);
    }

    /*
    //查询分润API id为医生id
    function get_profit(){
    $id = Request::input('id');
    $start = Request::input('start');
    $end = Request::input('end');

    if(!$id || !$start || !$end){
    return $this->res(1,'查询失败，参数错误');
    }

// 获得医生下两级的用户数组
$child_list = $this->get_child($id);

if(count($child_list) == 0){
return $this->res(0,'查询成功,无订单');
}
// 遍历用户数组查询用户订单 放进$data数组
$data = array();
for($i = 0;$i < count($child_list);$i++){
$result = $this->get_order($child_list[$i],$start,$end);

if(count($result) !== 0){
//return $this->res(0,'查询成功',$result);
$data = array_merge($data,$result);          
}       
}
$a = 0;
foreach($data as $val){
$a +=  (float)$val['prices'];        
}
return $this->res(0,'查询成功',$a);
}
     */
//查询订单API 默认当天时间 id为医生id
function get_search_order(){
    $id = Request::input('id');
    $start = Request::input('start');
    $end = Request::input('end');

    if(!$id || !$start || !$end){
        return $this->res(1,'查询失败，参数错误');
    }

    // 获得医生下两级的用户数组
    $child_list = $this->get_child($id);

    if(count($child_list) == 0){
        return $this->res(0,'查询成功,无订单');
    }
    // 遍历用户数组查询用户订单 放进$data数组
    $data = array();
    for($i = 0;$i < count($child_list);$i++){
        $result = $this->get_order($child_list[$i],$start,$end);

        if(count($result) !== 0){
            //return $this->res(0,'查询成功',$result);
            $data = array_merge($data,$result);          
        }       
    }
    if(count($data) == 0){
        return $this->res(0,'查询成功',[]);
    }
    return $this->res(0,'查询成功',$data);
}
//API 获得所有下级{id}
function get_all_child(){
    $id = Request::input('id');
    $child = $this->get_child($id);
    $result = array();
    foreach($child as $val){
        $a = array();
        $b = DoctorUser::where('id',$val)->get()->first();
        $a['id'] = $b['id'];
        $a['created_at'] = $b['created_at'];
        $a['name'] = $b['name'];
        array_push($result,$a);
    }
    return $this->res(0,'查询成功',$result);
}

//获得用户下两级 {id}
function get_child($id){
    //找到该用户的所有下级
    $lower = array();

    $result = DoctorUser::where('chief',$id)->get();
    for($i = 0; $i < count($result);$i++){
        array_push($lower,$result[$i]['id']);
    }
    $len = count($lower);
    for($i = 0; $i < $len; $i++){
        $result = DoctorUser::where('chief',$lower[$i])->get();
        for($j = 0; $j < count($result);$j++){
            array_push($lower,$result[$j]['id']);
        }
    }
    return $lower;
}

//查询用户订单 {from_id,start,end}
function get_order($from_id,$start,$end){
    $result = DoctorOrder::whereRaw('from_id = ? and created_at > ? and created_at <= ?',[$from_id,$start,$end])->get()->toArray();
    //$result = DoctorOrder::whereRaw('created_at > ?',['2021-07-15 00:00:00'])->get();
    $a = date('Y-m-d H:i:s');
    return $result;
}
// 封装response
function res($code,$msg,$data=null){
    if($data){
        return response()->json(array('code'=>$code, 'msg'=>$msg, 'data'=>$data));
    } else {
        return response()->json(array('code'=>$code, 'msg'=>$msg));
    }
}

}
